import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

import util.Config;
import util.Debug;
/**
 * Start class for the complete project.
 * Experiencing some problems with window handling ... :-(
 * TODO: fix it! 
 * @author Jolle
 * @version 1.0.0
 */
public class AndGo {
	
	private static JFrame frame = null;
	private static JCheckBox setDebugFlag = null;
	private static JCheckBox showDebugWindow = null;
	
	public static void main(String[] args) {
		
		frame = new JFrame("Main window");
		frame.setMinimumSize(new Dimension(260, 200));
		frame.setLayout(new GridLayout(5, 1, 15, 15));
		frame.setLocation(
				(Toolkit.getDefaultToolkit().getScreenSize().width  / 2) - 
				(frame.getWidth()  / 2), 
		 		(Toolkit.getDefaultToolkit().getScreenSize().height / 2) - 
		 		(frame.getHeight() / 2));
		frame.setResizable(false);
		
		JButton check = new JButton("Start DB Check");
		check.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				handleCheckBoxes();
				StartDB.main(new String[]{});
				if (Debug.frame != null) {
					Debug.frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				}
			}
		});
		frame.add(check);
		
		JButton insert = new JButton("Insert Weather Data");
		insert.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				handleCheckBoxes();
				InsertWeatherRun.main(new String[]{});
			}
		});
		frame.add(insert);
		
		JButton run = new JButton("Run JWeatherStation");
		run.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				handleCheckBoxes();
				frame.setVisible(false);
				JWeatherstationRun.main(new String[]{});
			}
		});
		frame.add(run);
		
		setDebugFlag = new JCheckBox("Set Debugging ON", Config.DEBUG);
		setDebugFlag.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				if (setDebugFlag.isSelected()) {
					showDebugWindow.setEnabled(true);
				}
				else {
					showDebugWindow.setSelected(false);
					showDebugWindow.setEnabled(false);
				}
			}
		});
		frame.add(setDebugFlag);
		
		showDebugWindow = new JCheckBox("Show Debug Window", true);
		frame.add(showDebugWindow);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.pack();
	}
	
	private static void handleCheckBoxes() {
		Config.DEBUG = setDebugFlag.isSelected();
		if (showDebugWindow.isSelected()) {
			Debug.show();
		}
	}
}
